﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DsLib;

namespace dsVisual
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        BinarySearchTree<string> _tree = new BinarySearchTree<string>();

        string text1Default = "Add Value";
        string text2Default = "Delete Value";
        string text3Default = "Search Value";

        bool keyboardFocus1 = false;
        bool keyboardFocus2 = false;
        bool keyboardFocus3 = false;

        private void textBox1_MouseEnter(object sender, MouseEventArgs e)
        {
            if (text1Default == textBox1.Text)
                textBox1.Text = "";
        }

        private void textBox1_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!keyboardFocus1)
            if (textBox1.Text == string.Empty)
                textBox1.Text = text1Default;
        }

        private void textBox1_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (text1Default == textBox1.Text)
                textBox1.Text = "";

            keyboardFocus1 = true;
        }

        private void textBox1_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (textBox1.Text == string.Empty)
                textBox1.Text = text1Default;

            keyboardFocus1 = false;
        }

        private void textBox2_MouseEnter(object sender, MouseEventArgs e)
        {
            if (text2Default == textBox2.Text)
                textBox2.Text = "";
        }

        private void textBox2_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!keyboardFocus2)
            if (textBox2.Text == string.Empty)
                textBox2.Text = text2Default;
        }

        private void textBox2_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (text2Default == textBox2.Text)
                textBox2.Text = "";

            keyboardFocus2 = true;
        }

        private void textBox2_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (textBox2.Text == string.Empty)
                textBox2.Text = text2Default;

            keyboardFocus2 = false;
        }

        private void textBox3_MouseEnter(object sender, MouseEventArgs e)
        {
            if (text3Default == textBox3.Text)
                textBox3.Text = "";
        }

        private void textBox3_MouseLeave(object sender, MouseEventArgs e)
        {
            if (!keyboardFocus3)
            if (textBox3.Text == string.Empty)
                textBox3.Text = text3Default;
        }

        private void textBox3_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (text3Default == textBox3.Text)
                textBox3.Text = "";

            keyboardFocus3 = true;
        }

        private void textBox3_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (textBox3.Text == string.Empty)
                textBox3.Text = text3Default;

            keyboardFocus3 = false;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (text1Default != textBox1.Text)
            {
                _tree.Add(textBox1.Text);

                string msg = "Added " + textBox1.Text + "\n";

                richTextBox1.AppendText(msg);
                
                textBox1.Text = text1Default;

                UpdateCount(_tree.Count);
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (text2Default != textBox2.Text)
            {
                bool deleted = _tree.Remove(textBox2.Text);

                string msg = deleted ? "Deleted " + textBox2.Text : textBox2.Text + " not found";
                msg += "\n";

                richTextBox1.AppendText(msg);

                textBox2.Text = text2Default;

                UpdateCount(_tree.Count);
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            if (text3Default != textBox3.Text)
            {
                bool exist = _tree.Contains(textBox3.Text);

                string msg = textBox3.Text + (exist ? " " : " not") + " found" + "\n";

                richTextBox1.AppendText(msg);

                textBox3.Text = text3Default;
            }
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            richTextBox1.Document.Blocks.Clear();
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            _tree = new BinarySearchTree<string>();

            UpdateCount(_tree.Count);

            richTextBox1.Document.Blocks.Clear();
        }

        private void UpdateCount(int count)
        {
            label2.Content = "Count: " + count.ToString();
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            string traversal;

            _tree.Traverse((TreeTraversalType)comboBox1.SelectedIndex, false, out traversal);

            richTextBox1.AppendText(traversal);
        }

        public class MyEnumToStringConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return value.ToString();
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                return (TreeTraversalType)Enum.Parse(typeof(TreeTraversalType), value.ToString(), true);
            }
        }
    }
}
